
using Youz.Common.DBUtility;
using Youz.Common.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.Common.DAL
{
    /// <summary>
    /// Right_Roleinfo Data Class
    /// </summary>
    public partial class Right_RoleinfoDAL : BaseDAL
    {
        private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "Right_Roleinfo";

        public Right_RoleinfoDAL()
        { }

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(Right_Roleinfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into [Right_Roleinfo](");
            strSql.Append("[RoleID],[RoleName],[RoleDesc],[Type],[IsDefault]");
            strSql.Append(") values (");
            strSql.Append("@RoleID,@RoleName,@RoleDesc,@Type,@IsDefault");
            strSql.Append(")");

            SqlParameter[] parameters = {
                  
                    new SqlParameter("@RoleID", model.RoleID),
                    new SqlParameter("@RoleName", model.RoleName),
                    new SqlParameter("@RoleDesc", model.RoleDesc),
                    new SqlParameter("@Type", model.Type),
                    new SqlParameter("@IsDefault", model.IsDefault)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(Right_Roleinfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [Right_Roleinfo] set ");
            strSql.Append("[RoleName]=@RoleName,[RoleDesc]=@RoleDesc,[ModifiedTime]=getdate(),[IsDefault]=@IsDefault");
            strSql.Append(" where [RoleID]=@RoleID");

            SqlParameter[] parameters = {
                  
                    new SqlParameter("@RoleID", model.RoleID),
                    new SqlParameter("@RoleName", model.RoleName),
                    new SqlParameter("@RoleDesc", model.RoleDesc),
                    new SqlParameter("@IsDefault", model.IsDefault)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(Guid RoleID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Right_Roleinfo] ");
            strSql.Append(" where [RoleID]=@RoleID");

            SqlParameter[] parameters = {
			
                new SqlParameter("@RoleID", RoleID)             
              
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Exists(SiteType type, string roleName)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select 1 from [Right_Roleinfo] where [Type]=@Type and RoleName=@RoleName");

            SqlParameter[] parameters = {
			    new SqlParameter("@Type", (int)type),
                new SqlParameter("@RoleName", roleName)             
              
            };

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Right_Roleinfo Get(Guid RoleID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 * from [Right_Roleinfo] ");
            strSql.Append(" where [RoleID]=@RoleID");

            SqlParameter[] parameters = {
			
                new SqlParameter("@RoleID", RoleID)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Right_Roleinfo Get(DataRow row)
        {
            if (row == null) return null;

            Right_Roleinfo model = new Right_Roleinfo();


            if (row["RoleID"] != DBNull.Value)
            {
                model.RoleID = (Guid)row["RoleID"];
            }

            if (row["RoleName"] != DBNull.Value)
            {
                model.RoleName = (String)row["RoleName"];
            }

            if (row["RoleDesc"] != DBNull.Value)
            {
                model.RoleDesc = (String)row["RoleDesc"];
            }

            if (row["Type"] != DBNull.Value)
            {
                model.Type = (SiteType)row["Type"];
            }

            if (row["IsSystem"] != DBNull.Value)
            {
                model.IsSystem = (Boolean)row["IsSystem"];
            }

            if (row["IsDefault"] != DBNull.Value)
            {
                model.IsDefault = (Boolean)row["IsDefault"];
            }

            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select * from [Right_Roleinfo] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得站点默认角色
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public Guid GetDefault(SiteType type)
        {
            var obj = DbHelperSQL.GetSingle(string.Format("select * from [Right_Roleinfo] where [Type]={0}", (int)type));

            return obj != null ? (Guid)obj : Guid.Empty;
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM [Right_Roleinfo] ");

            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if (filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [Right_Roleinfo] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, SiteType siteType, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [Right_Roleinfo]  ");
            //strSql.Append(" where [Type]=" + (int)siteType);
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" and " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by RoleID desc ", out count, cmdParms);
            total = count;
            return dt;
        }
        /// <summary>
        ///获得默认角色
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool GetDefault(Guid RoleID, int IsDefault, SiteType siteType)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select * from [Right_Roleinfo] where IsDefault=@IsDefault and Type=@siteType and   [RoleID]!=@RoleID ");
            SqlParameter[] parameters = {
			 new SqlParameter("@IsDefault", IsDefault),
              new SqlParameter("@siteType", siteType),
                new SqlParameter("@RoleID", RoleID)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
               return true;
            }
            else
            {
                return false;
            }
        }
        #endregion  成员方法
    }
}


